package com.bsming.code;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author magenm
 * @date 2018/7/17 12:32
 */
public class PomFile {

    private static String POM_FILE = "src/main/resources/pom.xml";
    private static String ROOT_POM_FILE = "pom.xml";

    public void genPom(String projectName, String pkgName, String modelPrefix, String modelName, String modelNameX) {
        try {
            SAXReader sr = new SAXReader();
            Document doc = sr.read(new File(POM_FILE));
            fillPom(doc, projectName, pkgName, modelPrefix, modelName, modelNameX);
            saveDoc(doc, modelPrefix + modelName + "/pom.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void genRootPom(String prefix, String[] models) {
        try {
            SAXReader sr = new SAXReader();
            Document doc = sr.read(new File(ROOT_POM_FILE));
            Element root = doc.getRootElement();
            Element modules = root.element("modules");
            if (modules == null) {
                modules = root.addElement("modules");
                for (int i = 0; i < models.length; i++) {
                    modules.addElement("module").setText(prefix + models[i]);
                }
            } else {
                // 检查是否存在modules，不存在添加，存在则跳过
                List<Element> elements = modules.elements();
                for (String model : models) {
                    String canModel = model;
                    for (Element element : elements) {
                        String eleValue = element.getStringValue();
                        if (eleValue.equals(prefix + canModel)) {
                            canModel = null;
                            break;
                        }
                    }
                    if (canModel != null) {
                        modules.addElement("module").setText(prefix + canModel);
                    }
                }
            }

            saveDoc(doc, "pom.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void fillPom(Document doc, String projectName, String pkgName,
                         String modelPrefix, String modelName, String modelNameX) {

        Element root = doc.getRootElement();
        Element parent = root.addElement("parent");
        parent.addElement("artifactId").setText(projectName);
        parent.addElement("groupId").setText(pkgName);
        parent.addElement("version").setText("1.0.0-SNAPSHOT");

        root.addElement("artifactId").setText(modelPrefix + modelName);
        root.addElement("groupId").setText(pkgName);
        root.addElement("version").setText("1.0.0-SNAPSHOT");
        if (modelName.endsWith("web")) {
            root.addElement("packaging").setText("war");
        } else {
            root.addElement("packaging").setText("jar");
        }

        if (modelNameX != null) {
            Element dependencies = root.addElement("dependencies");
            Element dependency = dependencies.addElement("dependency");
            dependency.addElement("groupId").setText(pkgName);
            dependency.addElement("artifactId").setText(modelPrefix + modelNameX);
            dependency.addElement("version").setText("1.0.0-SNAPSHOT");
        }

        //        Element plugins = root.addElement("build").addElement("plugins");
        //        Element plugin = plugins.addElement("plugin");
        //        plugin.addElement("groupId").setText("org.springframework.boot");
        //        plugin.addElement("artifactId").setText("spring-boot-maven-plugin");

    }

    private void saveDoc(Document doc, String s) throws Exception {
        OutputFormat opf = new OutputFormat("\t", true, "UTF-8");
        opf.setTrimText(true);
        XMLWriter writer = new XMLWriter(new FileOutputStream(s), opf);
        writer.write(doc);
    }

}
